import axios from 'axios';

const apiClient = axios.create({
  baseURL: '/api',
  headers: {
    'Content-Type': 'application/json',
  },
});

// 请求拦截器
apiClient.interceptors.request.use(
  (config) => {
    // 可以在这里添加认证token等
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
apiClient.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    // 在这里处理全局错误，例如401未授权等
    return Promise.reject(error);
  }
);

export default {
  // System Endpoints
  getSystemInfo() {
    return apiClient.get('/system/info');
  },
  getSystemStatus() {
    return apiClient.get('/system/status');
  },
  getSystemMetrics() {
    return apiClient.get('/system/metrics');
  },
  getSystemProcesses() {
    return apiClient.get('/system/processes');
  },

  // Docker Endpoints
  getDockerContainers() {
    return apiClient.get('/docker/containers');
  },
  getDockerImages() {
    return apiClient.get('/docker/images');
  },
  getContainerLogs(containerId) {
    return apiClient.get(`/docker/containers/${containerId}/logs`);
  },
  startContainer(containerId) {
    return apiClient.post(`/docker/containers/${containerId}/start`);
  },
  stopContainer(containerId) {
    return apiClient.post(`/docker/containers/${containerId}/stop`);
  },
  restartContainer(containerId) {
    return apiClient.post(`/docker/containers/${containerId}/restart`);
  },
  removeContainer(containerId) {
    return apiClient.delete(`/docker/containers/${containerId}/remove`);
  },

  // Docker镜像更新相关API (旧的保留兼容性)
  checkImageUpdates() {
    return apiClient.post('/docker/images/updates/check');
  },
  getImageUpdateStatus() {
    return apiClient.get('/docker/images/updates/status');
  },
  pullImageUpdate(imageInfo) {
    return apiClient.post('/docker/images/updates/pull', { image_info: imageInfo });
  },
  pullAllAutoUpdates() {
    return apiClient.post('/docker/images/updates/pull-all');
  },
  getImagesConfig() {
    return apiClient.get('/docker/images/config');
  },
  saveImagesConfig(config) {
    return apiClient.post('/docker/images/config', config);
  },
  addImageConfig(imageConfig) {
    return apiClient.post('/docker/images/config/add', imageConfig);
  },
  removeImageConfig(imageName) {
    return apiClient.delete(`/docker/images/config/${encodeURIComponent(imageName)}`);
  },
  getLocalImages() {
    return apiClient.get('/docker/images/local');
  },

  // 新的配置驱动Docker管理API
  getDockerServicesDetailed() {
    return apiClient.get('/docker/config/services');
  },
  checkDockerUpdatesDetailed() {
    return apiClient.post('/docker/config/check-updates-detailed');
  },
  pullServiceImage(serviceName) {
    return apiClient.post(`/docker/config/services/${serviceName}/pull`);
  },
  startServiceContainer(serviceName) {
    return apiClient.post(`/docker/config/services/${serviceName}/start`);
  },
  stopServiceContainer(serviceName) {
    return apiClient.post(`/docker/config/services/${serviceName}/stop`);
  },

  // Network Endpoints
  getNetworkSettings() {
    return apiClient.get('/network/settings');
  },
  scanWifi() {
    return apiClient.get('/network/wifi/scan');
  },
  connectWifi(config) {
    return apiClient.post('/network/wifi/connect', config);
  },

  // 新的镜像更新API
  get(url) {
    // 如果URL已经包含完整路径，去掉baseURL重复
    const cleanUrl = url.startsWith('/api/') ? url.substring(4) : url;
    return apiClient.get(cleanUrl);
  },
  post(url, data, config = {}) {
    // 如果URL已经包含完整路径，去掉baseURL重复
    const cleanUrl = url.startsWith('/api/') ? url.substring(4) : url;
    return apiClient.post(cleanUrl, data, config);
  },

  // 长超时的POST请求，用于耗时操作如Docker容器重建
  postWithLongTimeout(url, data, timeoutMs = 120000) {
    const cleanUrl = url.startsWith('/api/') ? url.substring(4) : url;
    return apiClient.post(cleanUrl, data, {
      timeout: timeoutMs // 默认2分钟超时
    });
  },
  
  // ... other endpoints
}; 